/**
 * @module utils/GridStyleUtil
 * @desc 电网式样工具
 */

import { getAllStyle } from '../../../mapUtils/StyleListUtil';
import { injectLayerMeta } from '../utils/LayerUtil';
import Layer from '../constant/Layer';
import LineStyle from '../layerstyle/LineStyle';
import SymbolStyle from '../layerstyle/SymbolStyle';
import CircleStyle from '../layerstyle/CircleStyle';

/**
 * 创建数据源
 * @param {*} map 地图对象
 * @param {String} sourceid 数据源Id
 */
export function createSource(map, sourceid, option={}) {
  if(!map.getSource(sourceid)) {
    map.addSource(sourceid, {
      type: 'geojson',
      data: {
        type: 'FeatureCollection',
        features: []
      },
      ...option
    });
  }
}

/**
 * 删除图层
 * @param {ITool} self 地图工具箱
 */
export function removeLayer(self) {
  self._layerIdsAll.forEach((layerid) => {
    self.getMap().removeLayer(layerid);
  });
}

/**
 * 创建线设备图层
 * @param {*} tool 电网展示工具
 * @param {*} map 地图对象
 * @param {*} style 式样
 * @param {String} sourceid 数据源Id
 * @param {String} layeridPrefix 图层Id前缀
 */
export function createLineLayer(tool, map, style, sourceid, layeridPrefix) {
  var lineStyle = new LineStyle(style);
  // 创建线图层
  let id = `${layeridPrefix}_gridstyle_layer_${style.type}_${style.sbzlx}`;
  tool._layerIds.push(id);
  tool._layerIdsAll.push(id);
  if(!map.getLayer(id)) {
    map.addLayer(
      injectLayerMeta(
        {
          id: id,
          type: 'line',
          source: sourceid,
          filter: ['all', ['==', 'transSbzlx', style.sbzlx * 1]],
          layout: lineStyle.getLayout(),
          paint: lineStyle.getPaint()
        },
        {
          enableLabelControl: false
        }
      )
    );
  }
  // 创建线标注图层
  var lineSymbolStyle = new SymbolStyle({...style, 'text_color': style.line_color});
  lineSymbolStyle.defaultLayout = {
    'symbol-placement': 'line',
    'icon-ignore-placement': true,
    'icon-anchor': 'center',
    'text-field': '{' + Layer.GlobalMarkLabelField + '}',
    'text-size': 12,
    'text-font': ['Microsoft YaHei Regular'],
    'text-offset': [0, 0],
    'text-max-width': 8,
    'text-allow-overlap': true,
    'text-ignore-placement': true

  };
  let labelLayerid = `${layeridPrefix}_gridstyle_layer_${style.type}_${style.sbzlx}_label`;
  tool._layerIdsAll.push(labelLayerid);
  if(!map.getLayer(labelLayerid)) {
    map.addLayer(
      injectLayerMeta(
        {
          id: labelLayerid,
          type: 'symbol',
          source: sourceid,
          filter: ['all', ['==', 'transSbzlx', style.sbzlx * 1]],
          layout: lineSymbolStyle.getLayout(),
          paint: lineSymbolStyle.getPaint()
        },
        {
          enableLabelControl: true
        }
      )
    );
  }
}

/**
 * 创建图标设备图层
 * @param {*} tool 电网展示工具
 * @param {*} map 地图对象
 * @param {*} style 式样
 * @param {String} sourceid 数据源Id
 * @param {String} layeridPrefix 图层Id前缀
 */
export function createSymbolLayer(tool, map, style, sourceid, layeridPrefix) {
  var symbolStyle = new SymbolStyle(style);
  let id = `${layeridPrefix}_gridstyle_layer_${style.type}_${style.sbzlx}`;
  tool._layerIds.push(id);
  tool._layerIdsAll.push(id);
  if(!map.getLayer(id)) {
    map.addLayer(
      injectLayerMeta(
        {
          id: id,
          type: 'symbol',
          source: sourceid,
          filter: ['all', ['==', 'transSbzlx', style.sbzlx * 1]],
          layout: symbolStyle.getLayout(),
          paint: symbolStyle.getPaint()
        },
        {
          enableLabelControl: true
        }
      )
    );
  }
}

/**
 * 创建点设备图层
 * @param {*} tool 电网展示工具
 * @param {*} map 地图对象
 * @param {*} style 式样
 * @param {String} sourceid 数据源Id
 * @param {String} layeridPrefix 图层Id前缀
 */
export function createPointLayer(tool, map, style, sourceid, layeridPrefix) {
  let id = `${layeridPrefix}_gridstyle_layer_${style.type}_${style.sbzlx}`;
  tool._layerIds.push(id);
  tool._layerIdsAll.push(id);
  var circleStyle = new CircleStyle(style);
  if(!map.getLayer(id)) {
    map.addLayer(
      injectLayerMeta(
        {
          id: id,
          type: 'circle',
          source: sourceid,
          filter: ['all', ['==', 'transSbzlx', style.sbzlx * 1]],
          layout: circleStyle.getLayout(),
          paint: circleStyle.getPaint()
        },
        {
          enableLabelControl: true
        }
      )
    );
  }
}

/**
 * 创建连接点图层
 * @param {*} tool 电网展示工具
 * @param {*} map 地图对象
 * @param {String} sourceid 数据源Id
 * @param {String} layeridPrefix 图层Id前缀
 */
export function createConnectionLayer(tool, map, sourceid, layeridPrefix) {
  let id = `${layeridPrefix}_GRID_CONNECTION_LABEL_${tool.id}`;
  tool._layerIds.push(id);
  tool._layerIdsAll.push(id);
  var symbolStyle = new SymbolStyle({});
  symbolStyle.defaultPaint = {
    'text_color': ['get', 'textColor']
  };
  if(!map.getLayer(id)) {
    map.addLayer(
      injectLayerMeta(
        {
          id: id,
          type: 'symbol',
          source: sourceid,
          filter: ['all', ['==', 'sourceType', 'ConnectionLabel']],
          layout: symbolStyle.getLayout(),
          paint: symbolStyle.getPaint()
        },
        { filter: ['all', ['==', 'sourceType', 'ConnectionLabel']] }
      )
    );
  }
}

/**
 * 创建图层
 * @param {ITool} self 地图工具箱
 */
export function createLayer(self) {
  let map = self.getMap();
  let sourceid = self._sourceId;
  let layeridPrefix = self.layeridPrefix;
  // 获取所有式样
  let styles = getAllStyle();
  // 创建图层
  self._layerIds = [];
  self._layerIdsAll = [];

  [...styles.lineStyle, ...styles.pointStyle].forEach((style) => {
    if (style.type === 'line') {
      createLineLayer(self, map, style, sourceid, layeridPrefix);
    } else if (style.type === 'marker') {
      createSymbolLayer(self, map, style, sourceid, layeridPrefix);
    } else if (style.type === 'point') {
      createPointLayer(self, map, style, sourceid, layeridPrefix);
    }
  });
  createConnectionLayer(self, map, sourceid, layeridPrefix);
}